﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class BookDetailsView : System.Web.UI.UserControl
{
    string code;
    string lib;
    int libID;
    Book book;
    Library library;
    Publisher publisher;
    CheckedOutBook coBook;
    ReservedBook reservedBook;
    ManageBook manageBook;
    ManageCheckedOutBook manageCheckedOutBook;
    ManageLibrary manageLibrary;
    ManagePublisher managePublisher;
    UtilityManager util;

    protected void Page_Load(object sender, EventArgs e)
    {
        lib = Request.QueryString["id"].ToString();
        libID = Convert.ToInt32(lib);
        code = Request.QueryString[1].ToString();
        manageBook = new ManageBook();
        manageCheckedOutBook = new ManageCheckedOutBook();
        manageLibrary = new ManageLibrary();
        managePublisher = new ManagePublisher();
        book = new Book();
        library = new Library();
        publisher = new Publisher();
        coBook = new CheckedOutBook();
        reservedBook = new ReservedBook();
        util = new UtilityManager();
        Label1.Text = util.getDate().ToShortDateString();
        pnlMoreDetails.Visible = false;
        pnlSimpleDetails.Visible = true;
        getDetails(libID, code);
    }

    protected void btnCopyDetails_Click(object sender, EventArgs e)
    {
        btnMoreDetails.CssClass = "notSelected";
        btnCopyDetails.CssClass = "selected";
        pnlMoreDetails.Visible = false;
        pnlSimpleDetails.Visible = true;
    }
    protected void btnMoreDetails_Click(object sender, EventArgs e)
    {
        btnCopyDetails.CssClass = "notSelected";
        btnMoreDetails.CssClass = "selected";
        pnlSimpleDetails.Visible = false;
        pnlMoreDetails.Visible = true;
    }

    private void getDetails(int lib, string code)
    {
        library = manageLibrary.getLibrary(lib.ToString());
        book = manageBook.getBookDetails(code);
        publisher = managePublisher.getPublisher(book.PublisherID);
        displayDetails();
    }

    private void displayDetails()
    {
        lblTitle.Text = book.Title;
        lblAuthor.Text = book.Author;
        lblPublisher.Text = publisher.Name;
        lblCallNumber.Text = book.CallNumber;
        lblLibrary.Text = library.Name;
        lblSeriesTitle.Text = book.SeriesTitle;
        lblSubject.Text = book.Subject;
        lblKeywords.Text = book.Keywords;
        lblDatePublished.Text = book.DatePublished.ToString();
        lblIsYoungAdult.Text = book.isYoungAdult.ToString();
        lblISBN.Text = book.ISBN.ToString();
        getBookStatus();
        
    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?id=" + lib);
    }

    protected void getBookStatus()
    {
        if (manageCheckedOutBook.isBookCheckedOut(book.Barcode) < 1)
            lblStatus.Text = coBook.dueDate.ToShortDateString();
        else if (manageCheckedOutBook.isBookRenewable(book.Barcode) < 1)
            lblStatus.Text = reservedBook.ReserveStatus;
        else
            lblStatus.Text = "Available";
    }
}
